//
//  LabelScrollView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/23.
//

import Foundation
import UIKit

class LabelScrollView: UIScrollView {
    private var timer: Timer? //消息滚动定时器
    private var msgOffsetY: CGFloat = 0 //消息横向滚动位置记录
    private var msgH: CGFloat = 0//消息宽度
    
    var modals: [TaskDeptRateModal] = [] {
        didSet {
            updateUI()
        }
    }
    
    var balzs: [BalzByDeptWdModel] = [] {
        didSet {
            updateBalzUI()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        showsVerticalScrollIndicator = false
        showsHorizontalScrollIndicator = false
        isScrollEnabled = false // 可以滚动
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateBalzUI() {
        self.removeAllSubViews()
        // 2.拿到文本的地方，计算文本高度
        msgH = CGFloat(balzs.count) * 25
        
        var lastL: UILabel?
        for (index, modal) in balzs.enumerated() {
            let textL = createBalzLabel(withModal: modal)
            addSubview(textL)
            textL.snp.makeConstraints { make in
                make.left.width.equalToSuperview()
                make.height.equalTo(20)
                make.top.equalTo(lastL != nil ? lastL!.snp.bottom : self.snp.top).offset(10)
                if index == modals.count - 1 {
                    make.bottom.equalTo(self.snp.bottom).offset(-10)
                }
            }
            
            lastL = textL
        }
        
        startTimer()
    }
    
    func updateUI() {
        self.removeAllSubViews()
        // 2.拿到文本的地方，计算文本高度
        msgH = CGFloat(modals.count) * 25
        
        var lastL: UILabel?
        for (index, modal) in modals.enumerated() {
            let textL = createLabel(withModal: modal)
            addSubview(textL)
            textL.snp.makeConstraints { make in
                make.left.width.equalToSuperview()
                make.height.equalTo(20)
                make.top.equalTo(lastL != nil ? lastL!.snp.bottom : self.snp.top).offset(10)
                if index == modals.count - 1 {
                    make.bottom.equalTo(self.snp.bottom).offset(-10)
                }
            }
            
            lastL = textL
        }
        
        startTimer()
    }

    // 3.定时器方法处理

    //MARK: ---- 开启定时器

    private func startTimer() {
        self.timer?.invalidate()
        self.timer = nil
        self.timer = Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(timerSelector), userInfo: nil, repeats: true)

        if let `timer` = timer {
            RunLoop.main.add(timer, forMode: .common)
        }
    }

    //MARK: ---- 定时器调用

    @objc private func timerSelector() {
        if msgH < msgH - 10 {
            msgOffsetY = 0
            self.setContentOffset(CGPoint.init(x: 0, y: 0), animated: false)
            return
        }
        if self.contentOffset.y >= msgH - 10 {
            msgOffsetY = 0
            self.setContentOffset(CGPoint.init(x: 0, y: 0), animated: false)
        }
        msgOffsetY += 1
        self.setContentOffset(CGPoint.init(x: 0, y: msgOffsetY), animated: true)
    }
    
    // MARK: ---- 显示详情label
    func createLabel(withModal modal: TaskDeptRateModal) -> UILabel {
        let l = UILabel()
        l.textAlignment = .left
        l.font = .systemFont(ofSize: 14)
        
        let value = "\(modal.zwcl ?? "0")%"
        let text = "\(modal.wdmc ?? "")  \(value)"
        let aAttri = NSMutableAttributedString(string: text, attributes: [.font:  UIFont.systemFont(ofSize: 14), .foregroundColor: UIColor.hex("#020202")])
        let aRange = NSMakeRange(text.distance(from: text.startIndex, to:text.range(of: value)!.lowerBound), value.count)
        aAttri.addAttribute(.foregroundColor, value: UIColor.hex("#EB7D4A"), range: aRange)
        l.attributedText = aAttri
        return l
    }
    
    func createBalzLabel(withModal modal: BalzByDeptWdModel) -> UILabel {
        let l = UILabel()
        l.textAlignment = .left
        l.font = .systemFont(ofSize: 14)
        
        let value = "\(modal.wcl ?? "0")%"
        let text = "\(modal.lzrmc ?? "")  \(value)"
        let aAttri = NSMutableAttributedString(string: text, attributes: [.font:  UIFont.systemFont(ofSize: 14), .foregroundColor: UIColor.hex("#020202")])
        let aRange = NSMakeRange(text.distance(from: text.startIndex, to:text.range(of: value)!.lowerBound), value.count)
        aAttri.addAttribute(.foregroundColor, value: UIColor.hex("#EB7D4A"), range: aRange)
        l.attributedText = aAttri
        return l
    }
}
